<template>
  <el-drawer v-model="isOpenWin" class="farm2-data-form" :append-to-body="true" :with-header="false" size="500px">
    <div class="farm-form-box">
      <el-form :show-message="true" :model="knowFrom" label-width="auto" ref="ruleFormRef" style="margin: 10px;">
        <div style="display: flex;">
          <el-form-item style="width: 100%;" prop="title" v-if="true" :rules="v.getRules(true, 0, 128)">
            <!--知识标题-->
            <el-input v-model="knowFrom.title" size="large" style="width: 100%" placeholder="请录入知识标题...">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <Postcard />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <ai_form_complete tip="直接生成15字以内的标题" :get-quote-html-handle="() => { return props.html }"
            :set-form-value-handle="(text: string) => { knowFrom.title = text; }"></ai_form_complete>
        </div>
        <!--id-->
        <div v-if="knowFrom.id" class="tip-title">
          ID:{{ knowFrom.id }}
        </div>
        <div style="display: flex;">
          <!--知识摘要-->
          <el-form-item style="width: 100%;" prop="docdescribe" v-if="true" :rules="v.getRules(false, 0, 512)">
            <el-input class="tip-input" v-model="knowFrom.docdescribe" style="width: 100%" autosize type="textarea"
              placeholder="请录入知识摘要..." />
          </el-form-item>
          <ai_form_complete tip="直接生成100字以内的摘要" :get-quote-html-handle="() => { return props.html }"
            :set-form-value-handle="(text: string) => { knowFrom.docdescribe = text; }"></ai_form_complete>
        </div>
        <!--标签-->
        <tag_form ref="tagFormRef" :know-tags="knowTags"></tag_form>
      </el-form>
      <div style="margin: 20px;">
        <hr />
      </div>
      <div style="text-align: center;">
        <el-button v-loading="isLoading" style="width: 80%;" size="large" @click="submitKnow" type="success" plain>
          <span v-if="!knowFrom.id"> 创建知识</span>
          <span v-if="knowFrom.id"> 更新知识</span>
        </el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script setup lang="ts">
import ai_form_complete from '../../ai/ai_form_complete.vue';
import tag_form from './tag_form.vue';
import Farm2Request from '@/service/remoteRequests/Farm2Request';
import v from '@/hook/farmFormValids';
import type { DataResponse } from '@/types/commons/DataResponse';
import { RequestTypeEnum } from '@/types/commons/RequestTypeEnum';
import { reactive, ref } from 'vue';
import farmRoutUtils from '@/hook/farmRoutUtils';
import notice from '@/components/msg/FarmNotice';
import type { Know } from '@/types/knows/Know';
import type { SkcTag } from '@/types/knows/SkcTag';
const isOpenWin = ref(false);
const tagFormRef = ref();
const isLoading = ref(false);//加载状态：true|false
const ruleFormRef = ref()//表单对象（可调用验证表单，清空表单方法）
const knowTags = reactive([]) as Array<SkcTag>;;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

const openWin = () => {
  isOpenWin.value = true;
};
const props = defineProps({
  html: {
    type: String,
    required: true,
  },
});
const knowFrom = reactive({
  text: props.html,
  title: '',
  id: '',
  docdescribe: '',
  tags: []
});
const setKnow = (cknow: Know) => {
  if (cknow.title) {
    knowFrom.title = cknow.title;
  }
  if (cknow.docdescribe) {
    knowFrom.docdescribe = cknow.docdescribe;
  }
  if (cknow.id) {
    knowFrom.id = cknow.id;
  }
  if (cknow.tags && cknow.tags.length > 0) {
    Object.assign(knowTags, cknow.tags);
  }
}


/**
 * 校验表单，准备提交
 * @param func
 */
const onSubmitForm = async (func: () => void) => {
  await ruleFormRef.value.validate((valid: boolean) => { if (valid) { func() } });
}

/**
 * 提交知识
 */
const submitKnow = () => {
  onSubmitForm(() => {
    knowFrom.text = props.html;
    knowFrom.tags = tagFormRef.value.getTags();
    Farm2Request.submit('api/wknow', RequestTypeEnum.post, isLoading, knowFrom).then((response: DataResponse) => {
      const know = response.data as { id: string };
      farmRoutUtils.openPageByPathId('/knowview', know.id, '_self');
    }).catch((msg: string) => {
      notice.byError(msg);//报错
    });
  });
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
defineExpose({
  openWin, setKnow
});
</script>
<style scoped>
/* 样式可以根据需要添加 */
.farm-form-box {
  padding: 20px;
}

.tip-title {
  font-size: 12px;
  margin-top: 20px;
  color: #dddddd;
}

.tip-input {
  margin-top: 20px;
}
</style>
